package com.qf.zyh.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.zyh.common.ResponseResult;
import com.qf.zyh.common.ResultType;
import com.qf.zyh.dao.MenuMapper;
import com.qf.zyh.entity.Menu;
import com.qf.zyh.services.MenuServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
public class MenuServicesImpl implements MenuServices {
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public List<Menu> getMenuByUid(Integer id) {
        if(id>0){
            List<Menu> ml = menuMapper.getMenuByUid(id);
            List<Menu> fu =new ArrayList<>();
            for (Menu menu : ml) {
                if (menu.getParentId()==null) {
                    fu.add(menu);
                }
            }
            for (Menu menu : fu) {
                List<Menu> ez = new ArrayList<>();
                for (Menu menu1 : ml) {
                    if (menu1.getParentId()!=null&&menu1.getParentId().equals(menu.getId())&&menu1.getType()!=2) {
                        ez.add(menu1);
                    }
                }
                menu.setChildren(ez);
            }
            return fu;
        }
        return null;
    }

    @Override
    public List<Menu> getMenu() {
        List<Menu> all = menuMapper.getAll();
        List<Menu> fu = new ArrayList<>();
        for (Menu menu : all) {
            if (menu.getParentId()==null) {
                fu.add(menu);
            }
        }
        for (Menu menu : fu) {
            List<Menu> son = new ArrayList<>();
            for (Menu sons : all) {
                if (sons.getParentId()!=null&&sons.getParentId().equals(menu.getId())) {
                    son.add(sons);
                }
            }
            menu.setChildren(son);
        }
        return fu;
    }

    @Override
    public ResultType getlist(Integer page, Integer limit) {
       // PageHelper.startPage(page,limit);
        List<Menu> all = menuMapper.getAll();
        //PageInfo pageInfo = new PageInfo(all);
        return new ResultType(0,"",all.size(),all);
    }

    @Override
    public ResponseResult delete(Integer[] ids) {
        menuMapper.deleteById(ids);
        return ResponseResult.getSuccess();
    }

    @Override
    public ResponseResult addMenu(Menu m) {
        int i = menuMapper.insertSelective(m);
        if (i>0) {
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("添加失败");
    }

    @Override
    public ResponseResult update(Menu m) {
        int i = menuMapper.updateByPrimaryKeySelective(m);
        if (i>0) {
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("修改失败");
    }

    @Override
    public Menu info(Integer id) {
        Menu menu = menuMapper.selectByPrimaryKey(id);
        if (menu!=null) {
            return menu;
        }
        return null;
    }

    @Override
    public List<Menu> tree() {
        List<Menu> all = menuMapper.getAll();
        List<Menu> fu = new ArrayList<>();
        for (Menu menu : all) {
            if (menu.getParentId()==null) {
                fu.add(menu);
            }
        }
        for (Menu menu : fu) {
            List<Menu> zi = new ArrayList<>();
            for (Menu menu1 : all) {
                if (menu1.getParentId()!=null&&menu1.getParentId().equals(menu.getId())){
                    zi.add(menu1);
                }
            }
            menu.setChildren(zi);
        }
        return fu;
    }
}
